<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改页面</title>
    <link rel="stylesheet" href="../../layui/css/layui.css" />
    <style>
        .layui-input {
            width: 220px;
        }
        .layui-form-item{
            margin-top: 5px;
            padding-bottom: 15px;
        }
    </style>
</head>
<body>
<form class="layui-form" id="form" lay-filter="upd">
    <input type="hidden" id="cid" name="cid">
    <div class="layui-form-item">
        <label class="layui-form-label">总金额</label>
        <div class="layui-input-inline">
            <input type="text" name="crental" id="crental" required  lay-verify="required" placeholder="请输入总金额" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">押金</label>
        <div class="layui-input-inline">
            <input type="text" name="ccash" id="ccash" required  lay-verify="ccash" placeholder="请输入租金" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">时间</label>
        <div class="layui-input-inline">
            <input type="text" name="ctime" id="ctime" required  lay-verify="required"  autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="btn" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    //Demo
    layui.use(['form','laydate'], function(){
        var form = layui.form;
        $().ready(function () {
            $.ajax({
                type: "post",
                url: "/ContractServlet?method=queryOne",
                data: {"cid": getQueryString("cid")},
                dataType: "json",
                success: function (da) {
                    if (da.code == 0) {
                        // form.val("upd",da.data);
                        // console.log(da.data);
                        $.each(da.data,function (update,u) {
                            $('#cid').val(u.cid);
                            $("#crental").val(u.crental);
                            $('#ccash').val(u.ccash);
                            $('#ctime').val(layui.util.toDateString(u.ctime,'yyyy-MM-dd'));
                        })
                    } else {
                        layer.msg("查询数据失败！");
                    }
                }
            })
        })


        //监听提交
        form.on('submit(formDemo)', function(data) {
            //自定义表单效验
            form.verify({
                ccash: function(value, item){ //value：表单的值、item：表单的DOM对象
                    var crental=$('#crental').val();
                    var ccash=$('#ccash').val();
                    if(ccash > crental){
                        layer.alert('租金不能大于总金额！');
                    }else{
                        //通过ajax提交后台
                        $.ajax({
                            url:'/ContractServlet?method=update',
                            type:"post",
                            data:$('#form').serialize(),
                            async:true,
                            dataType: 'json',
                            success:function (da) {
                                if (da.code===0){
                                    layer.alert("修改成功！", function () {
                                        parent.layer.closeAll();
                                    });
                                }else {
                                    layer.msg("修改失败！");
                                }
                            }
                        })
                    }
                }
            })

            return false;
        });
    })
    //获取传来的？后的 字段参数
    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
</script>
</body>
</html>